﻿using SmartForm;
using SmartForm.Common;
using SmartForm.Rule;
using SmartlForm.DAL;
using SmartlForm.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmartlForm.NetJs
{
    public partial class BowContainer
    {
        public void updateUseCount()
        {
            GroupDao.Instance.UpdateUseCount(getAccountId());
        }

        public void send(string title, string content, string type)
        {
            _instanceMainForm.BeginInvoke(new MethodInvokerPar3(startSend), title, content, type);
        }

        public void startSend(string title, string content, string type)
        {
            if (string.IsNullOrEmpty(type))
            {
                return;
            }
            List<SystemRule> systemRuleList = new List<SystemRule>();
            string[] types = type.Split(',');
            if (types.Contains("weibo"))
            {
                systemRuleList.Add(getWeiboRule(title, content));
            }
            TaskRun(systemRuleList, 0);
        }

        static bool Completed = false;

        static void TaskRun(List<SystemRule> systemRuleList, int index = 0)
        {
            SystemRule systemRule = systemRuleList[index];
            //可以判断系统使用浏览器版本
            SmartlForm.BowContainer mc = new SmartlForm.BowContainer(new FormPar() { title = "一键分发", windowMax = true, max = false, min = false });
            mc.Completed += TaskCompleted;
            Completed = false;
            mc.Show();
            if (systemRuleList.Count - 1 > index)
            {
                //等待完成后进行下一个任务
                WaitTaskCompleted();
                TaskRun(systemRuleList, index + 1);
            }
        }

        /// <summary>
        /// 任务完成回调
        /// </summary>
        static void TaskCompleted()
        {
            Completed = true;
        }


        /// <summary>
        /// 等待当前任务完成
        /// </summary>
        static void WaitTaskCompleted()
        {
            if (!Completed)
            {
                Delay.DelaySecond(1);
                WaitTaskCompleted();
            }
        }

        /// <summary>
        /// 获取微博规则
        /// </summary>
        private SystemRule getWeiboRule(string title, string content)
        {
            SystemRule systemRule = new SystemRule();
            systemRule.LoginUrl = "https://card.weibo.com/article/v3/editor";
            //系统规则
            systemRule.FormOptionJSList = new List<FormOptionJS>()
            {
                new FormOptionJS(){  JS="document.getElementsByClassName('bor')[0].click()", Second = 1 },
                new FormOptionJS(){  JS="document.getElementsByClassName('W_input W_input_focus')[0].value='$value'",Name="Title", Value="标题", Second = 1},
                new FormOptionJS(){  JS="document.getElementsByClassName('W_input_focus')[1].value='$value'",Name="Title", Value="导语", Second = 1},
                new FormOptionJS(){  JS="document.getElementsByClassName('WB_editor_iframe')[0].innerHTML='<p align='justify'>$value</p>'",Name="Content", Value="内容", Second = 1},
                new FormOptionJS(){  JS="document.getElementById('btnSave').click()", Value="智能表单 - 发表按钮",Submit=true,Completed=true},
            };

            return systemRule;
        }


        /// <summary>
        /// 今日头条
        /// </summary>
        private SystemRule getToutiaoRule(string title, string content)
        {
            SystemRule systemRule = new SystemRule();
            //系统规则
            systemRule.FormOptionJSList = new List<FormOptionJS>()
            {
                new FormOptionJS(){  JS="document.getElementsByClassName('bor')[0].click()", Second = 1 },
                new FormOptionJS(){  JS="document.getElementsByClassName('W_input W_input_focus')[0].value='$value'",Name="Title", Value="标题", Second = 1},
                new FormOptionJS(){  JS="document.getElementsByClassName('W_input_focus')[1].value='$value'",Name="Title", Value="导语", Second = 1},
                new FormOptionJS(){  JS="document.getElementsByClassName('WB_editor_iframe')[0].innerHTML='<p align='justify'>$value</p>'",Name="Content", Value="内容", Second = 1},
                new FormOptionJS(){  JS="document.getElementById('btnSave').click()", Value="智能表单 - 发表按钮",Submit=true,Completed=true},
            };

            return systemRule;
        }
    }
}
